package com.binvshe.binvshe.message.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.common.huanxin_im.ChatFragment;
import com.binvshe.binvshe.common.huanxin_im.ChatHelper;
import com.binvshe.binvshe.contacts.AddFriendActivity;
import com.binvshe.binvshe.contacts.FriendListActivity;
import com.binvshe.binvshe.message.MessageIndexActivity;
import com.binvshe.binvshe.organization.OrganizationFragment;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.easeui.adapter.EaseConversationAdapater;
import com.hyphenate.easeui.ui.EaseConversationListFragment;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.frameworkbase.dialog.AlertDialogFragment;
import com.pachong.android.frameworkbase.utils.SLog;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/9/19
 */
public class MessageListFragment extends BaseFragment {
    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_message_list, container, false);
    }


    EaseConversationListFragment conversationListFragment;
    ImageButton ringButton;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        showToolbar();
        setTitle("消息");
        ringButton = getCustomToolbar().addLeftImageButton(R.mipmap.icon_messagess, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showMessageRing(false);
//                MessageNoticeActivity.start(getContext());
                MessageIndexActivity.start(getContext());
            }
        });

        getCustomToolbar().addRightImageButton(R.mipmap.icon_add, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), AddFriendActivity.class);
                startActivity(intent);
            }
        });
        getCustomToolbar().addRightImageButton(R.mipmap.icon_address_book, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), FriendListActivity.class);
                startActivity(intent);
            }
        });

        conversationListFragment = new EaseConversationListFragment();
        conversationListFragment.setConversationListItemClickListener(new EaseConversationListFragment.EaseConversationListItemClickListener() {

            @Override
            public void onListItemClicked(EMConversation conversation) {


                if (conversation.getType() == EMConversation.EMConversationType.Chat) {
                    ChatHelper.startSingleChat(getActivity(), conversation.conversationId());
                    String fromId = conversation.getLastMessage().getFrom();
                } else if (conversation.getType() == EMConversation.EMConversationType.GroupChat) {
                    ChatHelper.startGroupChat(getActivity(), conversation.conversationId());
                } else  {
                    ChatFragment.log("点击的消息类型不是单聊也不是群聊");
                }
//                startActivity(new Intent(getActivity(), ChatActivity.class).putExtra(EaseConstant.EXTRA_USER_ID, conversation.getUserName()));
            }

        });

        conversationListFragment.setConversationListItemLongClickListener(new EaseConversationListFragment.EaseConversationListItemLongClickListener() {

            @Override
            public void onListItemLongClicked(final int position, final EMConversation conversation) {
                final AlertDialogFragment fragment = new AlertDialogFragment();
                fragment.setMessage("确定删除？").setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        fragment.dismiss();
                    }
                }).setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 删除conversation
                        EaseConversationAdapater adapater =  (EaseConversationAdapater)conversationListFragment.conversationListView.getAdapter();
                        conversationListFragment.conversationListView.conversations.remove(position);
                        EMClient.getInstance().chatManager().deleteConversation(conversation.conversationId(), false);
                        adapater.notifyDataSetChanged();
                        fragment.dismiss();
                    }
                }).show(getChildFragmentManager());
            }

        });


        getChildFragmentManager().beginTransaction().add(R.id.frlytMessageList, conversationListFragment).commitAllowingStateLoss();


        OrganizationFragment.setPaddingTop(getActivity(), getRootView());
        return view;
    }


    public void refresh() {
        conversationListFragment.refresh();
    }

    /**
     * 消息来了， 响铃
     */
    public void showMessageRing(boolean show) {
        if (show) {
            ringButton.setImageResource(R.mipmap.icon_messages_unread);
        } else {
            ringButton.setImageResource(R.mipmap.icon_messagess);
        }

    }

}
